package com.demo.springboothexadecimalconversion.hextobyte;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vccp.asn1.uper.BitBuffer;
import com.vccp.asn1.uper.ByteBitBuffer;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 16进制字符串转化为二进制数据
 *
 * @author Hanson
 * @date 2022/10/11 11:22
 */
public class hexStringToByte {

    /**
     * 忠哥 十六进制转换成二进制
     * @param s
     * @return
     */
    public static byte[] hexToByte(String s) {
        if (s.length() % 2 != 0) {
            throw new IllegalArgumentException("Converting to bytes requires even number of characters, got " + s.length());
        } else {
            int len = s.length();
            byte[] data = new byte[len / 2];

            for(int i = 0; i < len; i += 2) {
                data[i / 2] = (byte)((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
            }

            return data;
        }
    }

    /**
     * 测试模拟发送BSM消息
     */
    @Test
    public void test3(){
        String msg= "[121.627831, 31.284634],\n" +
                "[121.627831, 31.285161],\n" +
                "[121.627762, 31.285267],\n" +
                "[121.625455, 31.285294],\n" +
                "[121.625246, 31.285184],\n" +
                "[121.625224, 31.279894],\n" +
                "[121.625305, 31.279843],\n" +
                "[121.625396, 31.279875],\n" +
                "[121.625426, 31.285166],\n" +
                "[121.625474, 31.285198],\n" +
                "[121.627776, 31.285189],\n" +
                "[121.627808, 31.284501],\n" +
                "[121.627094, 31.284501],\n" +
                "[121.627094, 31.284295],\n" +
                "[121.627502, 31.284295],\n" +
                "[121.627502, 31.28452],\n" +
                "[121.627835, 31.284483],\n" +
                "[121.627845, 31.284744],";

        String[] arr = msg.split(",\\n");
        //System.out.println(split[0].substring(1));

        String jsonMsg = "{\"bsmDatas\":{\"posAccuracy\":{\"semiMinor\":0,\"orientation\":0,\"semiMajor\":0},\"secMark\":2246,\"heading\":7200,\"brakes\":{\"scs\":0,\"wheelBrakes\":\"00000\",\"abs\":0,\"traction\":0,\"brakeBoost\":0,\"auxBrakes\":0,\"brakePadel\":0},\"emergencyExt\":{\"responseType\":2,\"sirenUse\":0,\"lightsUse\":0},\"posConfidence\":{\"elevation\":{\"value\":0},\"pos\":{\"value\":0}},\"msgCnt\":33,\"timeConfidence\":0,\"speed\":500,\"safetyExt\":{\"events\":\"0000000000000\",\"lights\":\"000000000\"},\"transmission\":0,\"size\":{\"width\":0,\"length\":0,\"height\":0},\"pos\":{\"elevation\":0,\"lon\":0,\"lat\":0},\"accelSet\":{\"vert\":0,\"lon\":0,\"lat\":0,\"yaw\":0},\"angle\":0,\"vehicleClass\":{\"fuelType\":0,\"classification\":20},\"id\":\"93755347428146C3\",\"motionCfd\":{\"speedCfd\":{\"value\":0},\"steerCfd\":{\"value\":0},\"headingCfd\":{\"value\":0}}},\"msgFrame\":\"005\",\"seqNum\":\"123456\",\"ack\":true,\"length\":58,\"protocolVersion\":\"V2.0\",\"dId\":\"0001001123456789\",\"timestamp\":1608703317000}";
        Map<String,Object> resultMap = new HashMap<>();
        List<String> coordinateList = new ArrayList<>(2);

        for (int i = 0; i < arr.length; i++) {
            JSONObject jsonData = JSON.parseObject(jsonMsg);
            JSONObject bsmDatas = JSON.parseObject(jsonData.getString("bsmDatas"));
            JSONObject pos = JSON.parseObject(bsmDatas.getString("pos"));
            String[] split = arr[i].split(",");
            String lon = split[0].substring(1);
            String lat = split[1].substring(0,split[1].lastIndexOf("]"));

            pos.put("lon",lon);
            pos.put("lat",lat);
            bsmDatas.put("pos",pos);

            jsonData.put("bsmDatas",bsmDatas);
            System.out.println(jsonData);

        }
    }

    /**
     * 把字符串转换成字节数组
     */
    @Test
    public void StringToByte(){
        String s = "TEST2019";
        byte[] bytes = s.getBytes();
        String s1 = bytesToHexString(bytes);
        System.out.println(s1);
        String s2 = new String(bytes);
        System.out.println(s2);
    }

    private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static String bytesToHexString(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for(byte b : bytes) { // 利用位运算进行转换，可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }
        return new String(buf);
    }

}
